<?php

namespace App\Services;

use Flc\Dysms\Client;
use Flc\Dysms\Request\SendSms;
use Illuminate\Support\Facades\Log;

class ShortMessageService
{

    public function sendVerificationCode($mobile, $code): bool
    {
        return $this->sendSms($mobile, config('aliyun.sms.template_code'), ['code' => $code]);
    }

    public function sendSms($mobile, $templateCode, $params): bool
    {
        $client = new Client([
            'accessKeyId' => config('aliyun.sms.accessKeyId'),
            'accessKeySecret' => config('aliyun.sms.accessKeySecret'),
        ]);
        $sendSms = new SendSms();
        $sendSms->setPhoneNumbers($mobile);
        $sendSms->setSignName(config('aliyun.sms.sign_name'));
        $sendSms->setTemplateCode($templateCode);
        $sendSms->setTemplateParam($params);
        $res = $client->execute($sendSms);

        if ($res->Code !== 'OK') {
            Log::channel('sms')->info('__SMS_ERROR__:mobile='.$mobile . ',message=' . $res->Message);
            return false;
        }
        return true;
    }
}
